package com.hx.petsever.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hx.petsever.entity.Cart;
import com.hx.petsever.entity.CartVo;
import com.hx.petsever.mapper.CartMapper;
import com.hx.petsever.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    CartMapper cartMapper;
    
    @Override
    public HashMap DeleteOne(String userid, Integer goodid, Integer num) {
        HashMap<String , String > map = new HashMap<>();
        cartMapper.delete(userid,goodid,num);
        Cart cart = cartMapper.havaCart(userid, goodid);
        if(cart != null)
            map.put("msg","失败");
        else map.put("msg","成功");
        return map;
    }

    @Override
    public HashMap getAllbyId(String userid) {
        List<CartVo> cartlist = cartMapper.getCart(userid);
        HashMap<String , Object> map = new HashMap<>();
        if(cartlist.isEmpty())
        {
            map.put("msg","数据为空");
            map.put("state","0");
        }else {
            map.put("state",1);
            map.put("msg",cartlist);
        }
        return map;
    }

    @Override
    public HashMap Insert(String userid, Integer goodid, Integer num) {
        Cart cart = cartMapper.havaCart(userid, goodid);
        HashMap<String, String> map = new HashMap<>();
        if (cart != null)
        {
            cartMapper.upCart(userid,goodid,cart.getNum()+num);
        }else {
            cartMapper.addCart(userid,goodid,num);
        }
        map.put("data","成功");
        return map;
    }

    @Override
    public HashMap update(String userid, String goodid, String num) {
        HashMap<String, String> map = new HashMap<>();
        String goods[] = goodid.split(",");
        String nums[] = num.split(",");
        for(int i = 0 ; i < goods.length ; i++)
        {
            cartMapper.upCart(userid,Integer.parseInt(goods[i]),Integer.parseInt(nums[i]));
        }
        map = getAllbyId(userid);
        return map;
    }
}
